<?php

namespace app\admin\controller;

use app\common\model\Order as OrderModel;
use app\common\model\OrderRefund as OrderRefundModel;
/**
 * 订单控制器
 * Class Order
 * @package app\admin\controller
 */
class Order extends Base
{
    /**
     * [getIndex  获取订单]
     * @param type 1待付款 2已付款 3已完成 4已关闭 5全部订单
     * @param timetype 1下单时间 2付款时间 3出行时间 4所有时间
     * @param contype 1订单号 2商品名称
     * @return json
     * @author [C。]
     */
    public function getIndex()
    {
        $param = input('param.');
        $model = new OrderModel();
        $rmodel = new OrderRefundModel();
        $this->getPageAndSize($param);
        $map = [];
        $map1 = [];
        $user = $this->user;
        try{
            if(!empty($param['paytype'])){
                $map['o.paytype'] = $param['paytype'];
            }
            if(!empty($param['timetype'])){
                if($param['timetype'] == 1){
                    $map['o.create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
                }
                if($param['timetype'] == 2){
                    $map['o.paytime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
                }
                if($param['timetype'] == 3){
                    $map['o.gotime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
                }
                if($param['timetype'] == 4){
                    $map['o.gotime|o.create_time|o.paytime'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
                }
            }
            if(!empty($param['contype'])){
                if($param['contype'] == 1){
                    $map['o.ordersn'] = $param['con'];
                }
                if($param['contype'] == 2){
                    $map['g.title'] = $param['con'];
                }
            }
            $name = '全部订单';
            if($param['type'] == 1){
                $map['o.status'] = 0;
                $time = time() - 15*60;
                $map['o.create_time'] = ['gt',$time];
                $map['o.refundid'] = 0;
                $name = '待付款';
            }elseif($param['type'] == 2){
                $map['o.status'] = 1;
                $day = strtotime(date('Y-m-d'));
                $map['o.gotime'] = ['egt',$day];
                $map['o.refundid'] = 0;
                $name = '已付款';
            }elseif($param['type'] == 3){
                $map['o.status'] = 1;
                $day = strtotime(date('Y-m-d'));
                $map['o.gotime'] = ['lt',$day];
                $map['o.refundid'] = 0;
                $name = '已完成';
            }elseif($param['type'] == 4){
                $map['o.status'] = 0;
                $time = time() - 15*60;
                $map['o.create_time'] = ['elt',$time];
                $map1['o.status'] = 2;
                $name = '已关闭';
            }
            $field = 'o.id,o.create_time,o.ordersn,o.price,o.num,o.name,o.tel,o.status,o.people,o.gotime,g.title,g.cover,go.person_num,go.marketprice,o.refundid,o.paytype';
            if($param['type'] == 4){
                $count = $model->getCounts('',$time);//满足条件的总数
                $allpage = intval(ceil($count / $this->size));//总页数
                $lists = $model->getlists('',$time,$this->from, $this->size,$field);
            }else{
                $count = $model->getCount($map);//满足条件的总数
                $allpage = intval(ceil($count / $this->size));//总页数
                $lists = $model->getlist($map,$this->from, $this->size,$field);
            }

            foreach($lists as &$v){
                $v['people'] = json_decode(unserialize($v['people']));

                if($v['status'] == 0){
                    $time = time() - 3600*6;
                    if(strtotime($v['create_time']) > $time){
                        $v['state'] = '待付款';
                    }
                    if(strtotime($v['create_time']) <= $time){
                        $v['state'] = '已关闭';
                    }
                }
                if($v['status'] == 1){
                    $day = strtotime(date('Y-m-d'));
                    if($v['gotime'] >= $day){
                        $v['state'] = '已付款';
                    }
                    if($v['gotime'] < $day){
                        $v['state'] = '已完成';
                    }
                }
                if($v['status'] == 2){
                    $v['state'] = '已关闭';
                }
                if(!empty($v['refundid'])){
                    $refund = $rmodel->findData($v['refundid'],'id','status,refund_time');
                    if($refund['status'] == 0){
                        $v['state'] = '申请中';
                    }elseif($refund['status'] == 1){
                        $v['state'] = '已退款';
                        $v['refund_time'] = date('Y-m-d H:i:s',$refund['refund_time']);
                    }elseif($refund['status'] == 2){
                        $v['state'] = '拒绝退款';
                    }
                }
                $v['cover'] = explode(',',$v['cover']);
                $v['cover'] = $v['cover'][0];
                $v['gotime'] = date('Y-m-d',$v['gotime']);
//                $refund = $rmodel->findData($v['id'],'orderid','status');
//                if(!empty($refund)){
//                    if($refund['status'] == 0){
//                        $v['state'] = '退款中';
//                    }elseif($refund['status'] == 1){
//                        $v['state'] = '已退款';
//                    }
//                }
            }
        }catch(\Exception $e){
            return admin_json(-1, '获取失败'.$e->getMessage());
        }
        writelog('查看'.$name.'列表',1,$user);
        return admin_json(1,'获取成功',[
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [postEdit  修改订单]
     * @param type 1后台付款 2修改金额
     * @return json
     * @author [C。]
     */
    public function postEdit()
    {
        $param = input('param.');
        $model = new OrderModel();
        $user = $this->user;
        try{
            if($param['type'] == 1){
                $param['status'] = 1;
                $param['paytype'] = 3;
                $param['paytime'] = time();
            }
            $model->editData($param);
        }catch(\Exception $e){
            return admin_json(-1, '修改失败'.$e->getMessage());
        }
        if($param['type'] == 1){
            $name = '后台付款';
        }elseif($param['type'] == 2){
            $name = '修改金额';
        }
        writelog($name,1,$user);
        return admin_json(1, '修改成功');
    }
}